import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.Auth;

public class GetBucketDomains {

    private static final String ACCESS_KEY = "CgSNLoE32IJ0wqHYi4qlFMaVQol6gwdWpi3xYL5Y";
    private static final String SECRET_KEY = "RSldRTvDxZFEIX2jPzYT2mHP-KA3YlPaALYG9oYq";
    private static final String BUCKET_NAME = "yh-store";

    public static void main(String[] args) throws QiniuException {
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        // API endpoint for listing domains of a bucket
        String url = "https://api.qiniu.com/v6/domain/list";

        Client client = new Client();
        Response response = null;
        try {
            // 创建请求体
            String requestBody = String.format("{\"bucket\":\"%s\"}", BUCKET_NAME);

            // 生成鉴权头部
            long expireInSeconds = 3600; // 1小时过期时间
            String token = auth.signRequest(null, requestBody.getBytes(), String.valueOf(expireInSeconds));

            // 设置请求头
            String authHeader = "Qiniu " + token;

            // 发送POST请求并接收响应
            response = client.post(url, requestBody.getBytes(), null, authHeader);
            System.out.println(response.bodyString());
        } catch (QiniuException e) {
            e.printStackTrace();
            if (response != null) {
                System.out.println("HTTP Status Code: " + response.statusCode);
                System.out.println("Response Body: " + response.bodyString());
            }
        }
    }
}